package com.wzt.lianfirecontrol.adapter.xuncha.devicexuncha;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.ShowFragActivity;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.BannerModel;
import com.wzt.lianfirecontrol.bean.recode.function.xuncha.devicexuncha.XunChaDeviceBuildingModel;
import com.wzt.lianfirecontrol.bean.recode.function.xuncha.devicexuncha.XunChaDeviceFloorModel;
import com.wzt.lianfirecontrol.bean.recode.user.RoleConstant;
import com.wzt.lianfirecontrol.bean.recode.user.UserInfoModel;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.ToastUtils;
import com.wzt.lianfirecontrol.utils.Utils;

public class  XunChaDeviceSelectBuildAdapter extends BaseRecyclerAdapter<XunChaDeviceBuildingModel> {
    private Context context;

    private boolean isHistory = false;

    private String floorId;

    public String getFloorId() {
        return floorId;
    }

    public void setFloorId(String floorId) {
        this.floorId = floorId;
    }

    public boolean isHistory() {
        return isHistory;
    }

    public void setHistory(boolean history) {
        isHistory = history;
    }

    public XunChaDeviceSelectBuildAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_xuncha_device_select_build, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, XunChaDeviceBuildingModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout ll_item_content;
        private LinearLayout ll_title;
        private TextView tv_location;
        private TextView tv_status;
        private ImageView iv_expend;
        private RecyclerView rlv_floor_list;
        private XunChaDeviceSelectFloorAdapter xunChaDeviceSelectFloorAdapter;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.ll_title = itemView.findViewById(R.id.ll_title);
        myViewHolder.tv_location = itemView.findViewById(R.id.tv_location);
        myViewHolder.tv_status = itemView.findViewById(R.id.tv_status);
        myViewHolder.iv_expend = itemView.findViewById(R.id.iv_expend);
        myViewHolder.rlv_floor_list = itemView.findViewById(R.id.rlv_floor_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        myViewHolder.rlv_floor_list.setLayoutManager(linearLayoutManager);
        myViewHolder.xunChaDeviceSelectFloorAdapter = new XunChaDeviceSelectFloorAdapter(context);
        myViewHolder.xunChaDeviceSelectFloorAdapter.setHistory(isHistory);
        myViewHolder.xunChaDeviceSelectFloorAdapter.setFloorId(floorId);
        myViewHolder.rlv_floor_list.setAdapter(myViewHolder.xunChaDeviceSelectFloorAdapter);
    }

    private void setItemData(final MyViewHolder viewHolder, final XunChaDeviceBuildingModel data, int realPosition) {
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.ll_item_content.getLayoutParams();

        if (realPosition == 0) {
            if (isHistory) {
                layoutParams.setMargins(0, Utils.dip2px(context, 5f), 0, Utils.dip2px(context, 5f));
            } else {
                layoutParams.setMargins(0, 0, 0, Utils.dip2px(context, 10));
            }
        } else {
            if (isHistory) {
                layoutParams.setMargins(0, 0, 0, Utils.dip2px(context, 5f));
            } else {
                layoutParams.setMargins(0, 0, 0, Utils.dip2px(context, 10));
            }
        }

        if (isHistory){
            viewHolder.ll_item_content.setBackground(context.getResources().getDrawable(R.drawable.shape_gray_bg_frame));
            viewHolder.rlv_floor_list.setBackgroundColor(context.getResources().getColor(R.color.divider_line_color));
        }else {
            viewHolder.rlv_floor_list.setBackgroundColor(context.getResources().getColor(R.color.white));
        }
        viewHolder.tv_location.setText(data.getName());
        if ("0".equals(data.getDownExpand())){
            viewHolder.iv_expend.setImageResource(R.mipmap.down_expand);
            viewHolder.rlv_floor_list.setVisibility(View.VISIBLE);
        }else {
            viewHolder.iv_expend.setImageResource(R.mipmap.up_expand);
            viewHolder.rlv_floor_list.setVisibility(View.GONE);
        }
        if (StringUtils.isEmpty(data.getRecordStatus())){
            viewHolder.tv_status.setVisibility(View.GONE);
        }else {
            viewHolder.tv_status.setVisibility(View.VISIBLE);
            if ("0".equals(data.getRecordStatus())){
                viewHolder.tv_status.setText("待检查");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.change_status_color));
            }else if ("1".equals(data.getRecordStatus())){
                viewHolder.tv_status.setText("检查中");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.look_status_color));
            }else if ("2".equals(data.getRecordStatus())){
                viewHolder.tv_status.setText("已完成");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.main_color));
            }else if ("3".equals(data.getRecordStatus())){
                viewHolder.tv_status.setText("已过期");
                viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.hint_text_color));
            }
        }

        viewHolder.xunChaDeviceSelectFloorAdapter.clear();
        viewHolder.xunChaDeviceSelectFloorAdapter.addDatas(data.getChildren());
        viewHolder.xunChaDeviceSelectFloorAdapter.setOnItemClickListener(new OnItemClickListener<XunChaDeviceFloorModel>() {
            @Override
            public void onItemClick(View v, int position, XunChaDeviceFloorModel model) {
                if (!UserInfoModel.isSelfCompany(context)){
                 //   ToastUtils.showToast(context, ConstData.ERROR_COMPANY_MSG);
                    return;
                }
                UserInfoModel userInfoModel = UserInfoModel.getUserInfo(context);
                if (!userInfoModel.getRoleAlias().contains(RoleConstant.ROLE_INSPECTION)){
                    ToastUtils.showToast(context, "无权限");
                    return;
                }
                BannerModel bannerModel = new BannerModel();
                bannerModel.setActionType(ConstData.SHOWFRAG);
                bannerModel.setActionValue(ConstData.BARFLOOR);
                bannerModel.setActionTitle("设备巡检");
                Bundle bundle = new Bundle();
                bundle.putString(ConstData.BARTYPE, "8");
                bundle.putSerializable("XunChaDeviceFloorModel", model);
                if ("3".equals(model.getStatus())){//楼层已完成
                    bundle.putString(ConstData.ISDEVICECOMPLETE, "1");
                }
                ((ShowFragActivity)context).setClickAction(bannerModel, bundle);
            }
        });

        viewHolder.ll_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("0".equals(data.getDownExpand())) {
                    data.setDownExpand("1");
                }else {
                    data.setDownExpand("0");
                }
                notifyDataSetChanged();
            }
        });
    }
}
